package com.mmlc.meimei.lichengkongjian.home;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.google.zxing.Result;
import com.google.zxing.client.android.AutoScannerView;
import com.google.zxing.client.android.BaseCaptureActivity;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.ComfirmLoginActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.classify.FastGoodsDetailActivity;
import com.mmlc.meimei.lichengkongjian.classify.ProductInfoActivity;
import com.mmlc.network.filter.GoodsDetailRequestFilter;
import com.mmlc.network.filter.ShiyijiandysmRequestFilter;
import com.mmlc.network.responsebean.GoodsDetailResponseBean;
import com.mmlc.utils.ToastUtils;


/**
 * 模仿微信的扫描界面
 */
public class WeChatCaptureActivity extends BaseCaptureActivity {

    private static final String TAG = WeChatCaptureActivity.class.getSimpleName();
    private SurfaceView surfaceView;
    private AutoScannerView autoScannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zxing_scan);
        surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        autoScannerView = (AutoScannerView) findViewById(R.id.autoscanner_view);
    }

    @Override
    protected void onResume() {
        super.onResume();
        autoScannerView.setCameraManager(cameraManager);
    }

    @Override
    public SurfaceView getSurfaceView() {
        return (surfaceView == null) ? (SurfaceView) findViewById(R.id.preview_view) : surfaceView;
    }
    String skustr;
    @Override
    public void dealDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
        Log.i(TAG, "rawResult：" + rawResult.getText() + " --barcode：" + barcode + "--scaleFactor：" + scaleFactor);
//        playBeepSoundAndVibrate(true, false);
        if(rawResult.getText().startsWith("http")){
            Uri uri = Uri.parse(rawResult.getText());
            Intent it = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(it);

        }else if(rawResult.getText().startsWith("itryroom")){
            dysm(rawResult.getText());
        }else if(rawResult.getText().startsWith("qrlogin")){
            Intent intent=new Intent(this, ComfirmLoginActivity.class);
            intent.putExtra("login_code",rawResult.getText());
            startActivity(intent);
            finish();
        }
        else {
            skustr = rawResult.getText();
            initInfo();
        }
//        Toast.makeText(this, rawResult.getText(), Toast.LENGTH_LONG).show();

//        对此次扫描结果不满意可以调用
//        reScan();
    }

    private void dysm(String text) {
        ShiyijiandysmRequestFilter s=new ShiyijiandysmRequestFilter(App.activity);
        s.requestBean.roomqr=text;
        s.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GoodsDetailResponseBean>() {
            @Override
            public void onSuccess(GoodsDetailResponseBean result) {
                ToastUtils.showToast(result.getMessage());
                if(result.getCode()==0){
                    finish();
                }
            }
            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }
    public void initInfo() {
//        if(null != App.activity) {
            GoodsDetailRequestFilter filter = new GoodsDetailRequestFilter(App.activity);
            filter.requestBean.sku = skustr;
            filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GoodsDetailResponseBean>() {
                @Override
                public void onSuccess(GoodsDetailResponseBean result) {
                    if (result != null) {
                        String type = result.getData().getType();
                        if (type.equals("1")) {
                            Intent intent = new Intent(WeChatCaptureActivity.this, FastGoodsDetailActivity.class);
                            intent.putExtra("sku", skustr);
                            intent.putExtra("type", "sku");
                            intent.putExtra("scan", "scan");
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(WeChatCaptureActivity.this, ProductInfoActivity.class);
                            intent.putExtra("sku", skustr);
                            intent.putExtra("type", "sku");
                            intent.putExtra("scan", "scan");
                            startActivity(intent);
                        }
                        finish();
                    }
                }
                @Override
                public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                Intent intent = new Intent(WeChatCaptureActivity.this, ProductInfoActivity.class);
                intent.putExtra("sku", skustr);
                intent.putExtra("type", "sku");
                intent.putExtra("scan", "scan");
                startActivity(intent);
                }
            });
    }
}